package huoye.shops.huoye.operation.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;


import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import huoye.shops.base.BaseActivity;
import huoye.shops.base.PullToRefreshFragment;
import huoye.shops.huoye.operation.activity.OrderDetailsActivity;
import huoye.shops.huoye.operation.adapter.SellOrderAdapter;
import huoye.shops.huoye.operation.model.SellOrder;
import huoye.shops.huoye.operation.presenter.SellOrderPresenter;
import huoye.shops.huoye.operation.view.ISellOrderRefreshView;
import huoye.shops.widget.CommonHintDialog;


/**
 * 2018 - 08 - 10 .
 */
public class SellOrderFragment extends PullToRefreshFragment<SellOrder> implements ISellOrderRefreshView {

  //  状态（0：全部 1：待付款 2：待发货 3：已发货 4：已完成 5：退货中 6：已取消）


    public static final int TYPE_SELL_ORDER_ALL = 0; //全部
    public static final int TYPE_SELL_ORDER_NOT = 2; //未发货
    public static final int TYPE_SELL_ORDER_YES = 3; //已发货
    public static final int TYPE_SELL_ORDER_FINISH = 5; //已完成

    private int mType;
    private SellOrderPresenter mSellOrderPresenter;
    private String SendType = "";
    @Override
    public RecyclerViewAdapter createAdapter() {

        Bundle bundle = getArguments();
        mType = bundle.getInt(BaseActivity.EXTRAS_KEY_DATA);

        SellOrderAdapter sellOrderAdapter = new SellOrderAdapter(getActivity());
        sellOrderAdapter.setOnSellOrderListener(new SellOrderAdapter.OnSellOrderListener() {
            @Override
            public void onDetailsClick(SellOrder sellOrder) { //详情

            }

            @Override
            public void onDeleteClick(SellOrder sellOrder) {//删除
                showDeleteHint(sellOrder);
            }

            @Override
            public void onSendClick(SellOrder sellOrder) {//发货
                startActivity(OrderDetailsActivity.class,sellOrder);
               // mSellOrderPresenter.setOrderState(sellOrder.getOrderId(),"send",SendType);

            }
        });
        return sellOrderAdapter;
    }
/*
    @Override
    protected void onShowFragment() {
        super.onShowFragment();
        query();
    }*/

    @Override
    public void onResume() {
        super.onResume();
        if(mSellOrderPresenter != null){
            query();
        }
    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {
        super.initView(view, bundle);

        mSellOrderPresenter = new SellOrderPresenter();
        mSellOrderPresenter.addTaskListener(this);
        mSellOrderPresenter.onStart();
        query();
    }


    @Override
    public void query(Paging paging) {
        super.query(paging);
        mSellOrderPresenter.getOrderList( mType,paging);

    }


    public static SellOrderFragment getInstan(int type) {
        SellOrderFragment fragment = new SellOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BaseActivity.EXTRAS_KEY_DATA, type);
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    public void onItemClick(SellOrder sellOrder, int i, View view) {
            startActivity(OrderDetailsActivity.class,sellOrder);
    }

    @Override
    public void refreshPage() {
        if(mSellOrderPresenter != null){
            query();
        }
    }


    private void showDeleteHint(SellOrder sellOrder){
        CommonHintDialog commonHintDialog = new CommonHintDialog(getActivity());
        commonHintDialog.setOnCommonHintListener(new CommonHintDialog.OnCommonHintListener() {
            @Override
            public void onLeftClick() {

            }

            @Override
            public void onRightClick() {
                mSellOrderPresenter.setOrderState(sellOrder.getOrderId(),"cancel",SendType);
            }
        });
        commonHintDialog.show();
        commonHintDialog.setDialogContent("确定要删除该订单？","取消","确定");

    }
}
